/**
 * 
 */
package com.maywide.jdp.system.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.maywide.jdp.core.vo.KeyValue;
import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.dao.Dao;
import com.maywide.jdp.dao.PageQueryTemplate;
import com.maywide.jdp.system.entities.Directory;

/**
 * 字典管理Bean
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Component("directoryBean")
@Transactional(propagation=Propagation.REQUIRED)
public class DirectoryBeanImpl implements DirectoryBean {
	/**
	 * 基础DAO
	 */
	private Dao dao = null;

	/**
	 * @param dao the dao to set
	 */
	@Autowired
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	@Override
	public void insert(Object obj) throws Exception {
		dao.insert(obj);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void remove(Class clazz,Serializable id) throws Exception {
		Object obj = dao.find(clazz,id);
		dao.remove(obj);
	}

	@Override
	public void update(Object obj) throws Exception {
		dao.update(obj);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Object load(Serializable id) throws Exception {
		Directory directory = dao.find(Directory.class, id);
		return directory;
	}

	@Override
	public void lock(String id) throws Exception {
		String jql = "update Directory as o set o.active = false where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void lockAll(String[] ids) throws Exception {
		String hql = "update Directory as b set o.active = false where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String,Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql,conditions);
	}

	@Override
	public void unlock(String id) throws Exception {
		String jql = "update Directory as o set o.active = true where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void unlockAll(String[] ids) throws Exception {
		String hql = "update Directory as b set o.active = true where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String,Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql,conditions);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Page page(int first, int limit, String sort, String dir,
			Map<String, Object> conditions,Map<String, Object> parameters) throws Exception {
		Page page = null;
		
		String queryString = "select distinct o from Directory as o";
		String countQueryString = "select count(distinct o.id) from Directory as o";
		PageQueryTemplate template = new PageQueryTemplate(queryString,countQueryString,first,limit,sort,dir);
		page = dao.executePageQuery(template,conditions,null);
		
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<KeyValue> findByDomainCode(String domainCode)
			throws Exception {
		String name = "com.maywide.jdp.system.entities.Directory.findByDomainCode";
		Map<String, Object> parameters = new HashMap<String,Object>();
		parameters.put("domainCode", domainCode);
		return dao.executeNamedQuery(name, null,parameters,KeyValue.class);
	}
}
